﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ZaidimasSandelys.CustomDrawTools;
using ZaidimasSandelys.Models.Classes;
using ZaidimasSandelys.Models.DataStructures;
using ZaidimasSandelys.Models.Interfaces;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DiplominioDarboTestai
{
    /// <summary>
    /// Entity Framework testai
    /// </summary>
    [TestClass]
    public class UnitTest2
    {
        private static readonly string CONN_STRING_KEY = "ZaidimasSandelysEntities";
        private IAppData _appData;

        [TestInitialize()]
        public void OpenConnection()
        {
            var connString = ConfigurationManager.ConnectionStrings[CONN_STRING_KEY].ConnectionString;
            _appData = new AppSqlCeDataEF(connString);
        }

        [TestMethod]
        public void TestConnection1EF()
        {
            IEnumerable<MainMenuItem> items = _appData.GetMainMenu();

            Assert.IsTrue(items != null);
            foreach (MainMenuItem itm in items)
            {
                System.Console.WriteLine("{0}\t{1}", itm.Text, itm.Value);
            }
        }

        [TestMethod]
        public void TestConnection1aEF()
        {
            IEnumerable<string> items = _appData.GetStartButtonText();

            Assert.IsTrue(items != null);
            foreach (string itm in items)
                System.Console.WriteLine(itm);
        }

        [TestMethod]
        public void TestConnection2EF()
        {
            IEnumerable<VariantMenuItem> items = _appData.GetGameVariants(GameLevel.Medium);

            Assert.IsTrue(items != null);
            foreach (VariantMenuItem itm in items)
            {
                System.Console.WriteLine("{0}\t{1}", itm.Text, itm.Value);
            }
        }

        [TestMethod]
        public void TestConnection3EF()
        {
            string[] items = _appData.GetMultilanguageConstants("MainWnd");
            Assert.IsTrue(items != null && items.Length > 0);
            Array.ForEach(items, x => { System.Console.WriteLine("{0}", x); });
            items = _appData.GetMultilanguageConstants("YouWon");
            Assert.IsTrue(items != null && items.Length > 0);
            Array.ForEach(items, x => { System.Console.WriteLine("{0}", x); });
        }

        [TestMethod]
        public void TestLoadGame1EF()
        {
            var game = new CurrentGame(_appData);

            game.LoadGame(0, 1, null);
            Assert.IsNotNull(game.Header);

            System.Console.WriteLine("id={0} Level={1} Variant={2}, X={3} Y={4}",
                game.Header.Id, game.Header.Level, game.Header.Variant,
                (int)game.Header.Person.X, (int)game.Header.Person.Y);
        }
        /// <summary>
        /// Žaidimo elementai kraunami tiesiai iš duomenų bazės metodo.
        /// </summary>
        [TestMethod]
        public void TestLoadGame2EF()
        {
            var game = new CurrentGame(_appData);
            var fakeList = new List<IGameElement>();

            game.LoadGame(0, 1, null);
            Assert.IsNotNull(game.Header);

            fakeList.AddRange(_appData.LoadVisualElements(game.Header.Id, VisualType.OuterSpace));
            Assert.IsTrue(fakeList.Count > 0);

            System.Console.WriteLine("id={0} Level={1} Variant={2}, X={3} Y={4}",
                game.Header.Id, game.Header.Level, game.Header.Variant,
                (int)game.Header.Person.X, (int)game.Header.Person.Y);
            fakeList.ForEach(x =>
            {
                VisualType visual = AllocatedVisual.SafeConvertToVisualType(x.ImgTypeCode);
                System.Console.WriteLine("\tPoint=({0},{1}) {2}", x.X, x.Y, visual.ToString());
            });
        }
/*
        //private List<AllocatedVisual> fakeList = null;
        //private AutoResetEvent event_1 = null;  //
        /// <summary>
        /// Išorinius elementus asinchroniškai užkrauna žaidimo procesorius, <see cref="CurrentGame"/>
        /// </summary>
        [TestMethod]
        public void TestLoadGame3EF()
        {
            var fakeList = new List<AllocatedVisual>();
            var event_1 = new AutoResetEvent(false);  //

            var game = new CurrentGame(_appData);
            //            game.SetFlagTest(0x0001);           // Skaityti tik išorinius laukus
            //            game.SetFlagTest(0x0002);           // Skaityti tik sienas
                        game.SetFlagTest(0x0004);           // Skaityti tik grindis
            //            game.SetFlagTest(0x0008);           // Skaityti tik taikinius (langeliai kuriuose talpinamos dėžės)
            //            game.SetFlagTest(0x0010);           // Skaityti tik dėžes
            game.LoadCompleted += delegate(object sender, LoadCompletedEventArgs e)
            {
                Assert.IsNotNull(game.Header);
                Assert.IsTrue(fakeList.Count > 0);
                int i = 1;

                fakeList.ForEach(x =>
                {
                    System.Console.WriteLine("{0}\tPoint=({1},{2}) {3}", i++, x.Position.Width, x.Position.Height, x.GetType().Name);
                });
                event_1.Set();
            };

            game.LoadGame(0, 1, fakeList);
            event_1.WaitOne();
        }
 */
    }

}
